package com.yy.controller;

import java.io.BufferedReader;
import java.io.PrintWriter;

import com.opensymphony.xwork2.ActionSupport;
import com.yy.dto.JobDTO;
import com.yy.logic.IJobLogic;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yy.dto.CompanyDTO;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

public class JobController extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	private JobDTO jobDTO;
	private CompanyDTO companyDTO;
	@Resource
	private IJobLogic jobLogic;
	
	
	public JobDTO getJobDTO() {
		return jobDTO;
	}


	public void setJobDTO(JobDTO jobDTO) {
		this.jobDTO = jobDTO;
	}


	public void getAlljob()throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();//取得reponse实例
		response.setCharacterEncoding("utf-8");//设置输出的编码
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();//得到printwriter实例
		
		Integer jobcompany = jobDTO.getJobCompany();
		
		System.out.println("CompanyId="+jobcompany);
		try{
			System.out.println("THIS IS JobController Controller 1");
			out.println(this.jobLogic.getAllJob(jobcompany));
			System.out.println("THIS IS Job Controller 2");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	public void alterJob()throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpServletRequest request=ServletActionContext.getRequest();
		System.out.println("********This is alterResume controller**********");
		request.setCharacterEncoding("UTF-8");
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = request.getReader();
		char[]buff = new char[1024];
		int len;
		while((len = reader.read(buff)) != -1) {
			  sb.append(buff,0, len);
		}
		System.out.println(sb.toString());
		String mark = sb.toString();
		System.out.println(mark);
		System.out.println(mark.length());
		JSONObject dataJson = new JSONObject(mark);
		int Jid=dataJson.getInt("Jid");
		String Jname=dataJson.getString("Jname");
		int  Jnum=dataJson.getInt("Jnum");
		int  Jhave=dataJson.getInt("Jhave");
		
	//	Integer jobcompany = jobDTO.getJobCompany();
		
	//	System.out.println("CompanyId="+jobcompany);
		try{
			System.out.println("THIS IS alertJob Controller 1");
			out.println(this.jobLogic.changeJob(Jid,Jname,Jnum,Jhave));
			System.out.println("THIS IS alertJob 2");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	
	public void deleteJob()throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpServletRequest request=ServletActionContext.getRequest();
		System.out.println("********This is alterResume controller**********");
		request.setCharacterEncoding("UTF-8");
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = request.getReader();
		char[]buff = new char[1024];
		int len;
		while((len = reader.read(buff)) != -1) {
			  sb.append(buff,0, len);
		}
		System.out.println(sb.toString());
		String mark = sb.toString();
		System.out.println(mark);
		System.out.println(mark.length());
		JSONObject dataJson = new JSONObject(mark);
		int Jid=dataJson.getInt("Jid");
		
		
		//System.out.println("CompanyId="+jobcompany);
		try{
			System.out.println("THIS IS delete Controller 1");
			out.print(this.jobLogic.delJob(Jid));
			System.out.println("THIS IS delete 2");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
	public void addJob()throws Exception{
		JobDTO jobDTO = new JobDTO();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		HttpServletRequest request=ServletActionContext.getRequest();
		System.out.println("********This is alterResume controller**********");
		request.setCharacterEncoding("UTF-8");
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = request.getReader();
		char[]buff = new char[1024];
		int len;
		while((len = reader.read(buff)) != -1) {
			  sb.append(buff,0, len);
		}
		System.out.println(sb.toString());
		String mark = sb.toString();
		System.out.println(mark);
		System.out.println(mark.length());
		JSONObject dataJson = new JSONObject(mark);
		System.out.println("111111");
		
		String Jname=dataJson.getString("Jname");
		System.out.println("22222");
		int Jcompany=dataJson.getInt("Jpany");
		System.out.println("33333");
		int  Jnum=dataJson.getInt("Jnum");
		System.out.println("44444");
		int  Jhave=dataJson.getInt("Jhave");
		System.out.println("55555");
		jobDTO.setJobName(Jname);
		System.out.println("55555");
		jobDTO.setJobCompany(Jcompany);
		jobDTO.setJobNum(Jnum);
		jobDTO.setJobHave(Jhave);
		try{
			System.out.println("THIS IS add Controller 1");
			out.println(this.jobLogic.addJob(jobDTO));
			System.out.println("THIS IS add 2");
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
		}
	}
}
